<template>
	<view class="dialog-login" :hidden="!show" @tap="onCloseLoginDialog">
		#<!-- #ifdef MP-WEIXIN -->
		<view class="dialog-body" @tap.stop="onDialogBody">
			<view class="title">请选择登录方式</view>
			<view class="content">
				<button type="primary" open-type="getUserInfo" @getuserinfo="onWechatLogin">微信登录</button>
			</view>
		</view>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<view class="dialog-body2">
			<view class="title">请用手机号码登入</view>
			<form  class="content" @reset="formreset" @submit="formSubmit">
				<input type="number" focus="true" :value="user.phone" name="phonenum" placeholder="手机号码"></input>
				<view class="yan">
					<input :value="user.testnum" name="testnum" placeholder="验证码"></input>
					<view class="sendactive" v-if="canuse" @tap.stop="gettestnum">发送验证码</view>
					<view class="send" v-if="!canuse">{{time}}s后重新发送</view>
				</view>
				<view class="sub">
					<button form-type="submit">登入</button>
					<button form-type="reset">重置</button>
				</view>
			</form>
		</view>
		<!-- #endif -->
		
	</view>
</template>

<script>
	// pages/component/login/login.js
	var util = require("../../../utils/util.js"); // var api = require('../../config/api.js');
	// var api = require('../../config/api.js');
	var app = getApp();

	export default {
		data() {
			return {
				canuse: "true",
				time: 60.,
				user:{
					phone:'',
					testnum:''
				}
			};
		},

		components: {},
		props: {
			show: {
				type: Boolean
			}
		},
		methods: {
			onCloseLoginDialog() {
				this.setData({
					show: false
				});
			},
			gettestnum() {
				if (this.canuse) {
					this.canuse = false;
					console.log(this.canuse)
					let that = this;
					// uni.showLoading({
					// 	title: '发送中...'
					// })
					// uni.request({
					// 	url: ,
					// 	key: value,
					// 	success: function(res) {
					// 		uni.hideLoading;

					// 	},
					// })
					//下面的setInterval应该放在success内
					var timer = setInterval((function() {
						that.canuse = true;
						clearInterval(timer);
					}), 60000)
					var interval = setInterval(() => {
						console.log(that.time)
						that.time --;
						if (that.time == -1) {
							clearInterval(interval);
							that.time = 60;
						}
					},1000);
					console.log(this.canuse)
				} else {}
			},
			 formSubmit: function(e) {
			                console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
			                var formdata = e.detail.value
			                uni.showModal({
			                    content: '表单数据内容：' + JSON.stringify(formdata),
			                    showCancel: false
			                });
			},
			formReset: function(e) {
			                console.log('清空数据')
			},
			onWechatLogin: function(e) {
				// console.log("==onWechatLogin=="+JSON.stringify(e))
				if (e.detail.errMsg !== 'getUserInfo:ok') {
					if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
						return false;
					}

					uni.showToast({
						title: '微信登录失败'
					});
					return false;
				}

				uni.showLoading({
					title: '登陆中...'
				});
				util.loginByWeixin().then(res => {
					uni.hideLoading(); // 设置用户信息

					if (res.errno == 0) {
						this.setData({
							show: false
						});
						this.$emit("success", {
							detail: app.globalData.userInfo
						});
					}
				});
			}
		}
	};
</script>
<style>
	.dialog-login {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}


	.dialog-login .dialog-body {
		width: 540rpx;
		height: auto;
		overflow: hidden;
		background: #fff;
		border-radius: 5px;
		padding: 40rpx;
	}

	.dialog-login .dialog-body2 {
		width: 600rpx;
		height: auto;
		overflow: hidden;
		background: #fff;
		border-radius: 5px;
		padding: 40rpx;
	}

	.dialog-login .dialog-body .title {
		width: 100%;
		text-align: center;
		line-height: 60rpx;
	}

	.dialog-login .dialog-body2 .title {
		width: 100%;
		text-align: center;
		line-height: 60rpx;
		font-size: 32rpx;
	}

	.dialog-login .dialog-body2 .content {
		width: 100%;

	}

	.dialog-login .dialog-body .content {
		width: 100%;
	}

	.dialog-login .dialog-body button {
		margin-top: 30rpx;
	}

	.dialog-login .dialog-body2 input {
		border: 2rpx solid #B28850;
		height: 80upx;
		line-height: 80 upx;
		/* letter-spacing: 6rpx; */
		padding-left: 20rpx;
		/* border-radius: 15rpx; */
		text-align: center;
	}

	.dialog-login .dialog-body2 .content .yan {
		display: flex;
		margin-top: 30rpx;
	}

	.dialog-login .dialog-body2 .content .yan .sendactive {
		width: 240rpx;
		margin-left: 40rpx;
		line-height: 80rpx;
		background: #B28850;
		text-align: center;
		color: #fff;
		/* border-radius: 15rpx; */
	}

	.dialog-login .dialog-body2 .content .yan .send {
		width: 300rpx;
		margin-left: 40rpx;
		line-height: 80rpx;
		background: #f1f1f1;
		text-align: center;
		color:#353535;
		/* border-radius: 15rpx; */
	}

	.dialog-login .dialog-body2 .content .sub {
		margin-top: 30rpx;
		display: flex;
	}
	.dialog-login .dialog-body2 .content .sub button{
		font-size: 30rpx;
		width: 150rpx;
		text-align:center;
		/* border: 2rpx solid #B28850; */
	}
</style>
